package com.basic.common.core.service.impl;

import com.basic.business.common.constants.Constants;
import com.basic.business.common.redis.RedisCacheUtils;
import com.basic.common.core.domain.entity.SysUserInfo;
import com.basic.common.core.mapper.SysUserMapper;
import com.basic.common.core.service.ISysUserService;
import org.springframework.stereotype.Service;

import javax.annotation.PostConstruct;
import javax.annotation.Resource;
import java.util.List;

@Service
public class SysUserServiceImpl implements ISysUserService {

    @Resource
    private SysUserMapper userMapper;


    public List<SysUserInfo> getNormalUserInfo() {
        // 从Redis中获取，若没有信息则从数据库中查询
        List<SysUserInfo> userInfos = RedisCacheUtils.getCacheObject(Constants.USER_REDIS_KEY);
        if (userInfos == null) {
            userInfos = userMapper.getNormalUserInfo();
        }
        return userInfos;
    }

    @PostConstruct
    public void init(){
        resetUserCache();
    }

    public void resetUserCache() {
        // 清空缓存
        RedisCacheUtils.clearCache(Constants.USER_REDIS_KEY + "*");

        RedisCacheUtils.setCacheObject(Constants.USER_REDIS_KEY, getNormalUserInfo());
    }

}
